package article

import (
	"gohub/pkg/app"
	"gohub/pkg/database"
	"gohub/pkg/paginator"

	"github.com/gin-gonic/gin"
)

// Get 通过 ID 获取
func Get(idstr string) (articleModel Article) {
	database.DB.Where("id", idstr).First(&articleModel)
	return
}

// All 获取所有数据
func All() (Articles []Article) {
	database.DB.Order("id").Find(&Articles)
	return
}

// Paginate 分页
func Paginate(c *gin.Context, perPage int) (articles []Article, paging paginator.Paging) {
	config := paginator.Config{
		//SortParam: "ranking",
		// ...其他配置
	}
	paginator := paginator.New(c, database.DB.Order("ranking").Model(&Article{}), app.V1URL(database.TableName(&Article{})), config)
	paging = paginator.Paginate(&articles)
	return
}

// func PaginateBak(c *gin.Context, perPage int) (articles []Article, paging paginator.Paging) {
// 	paging = paginator.Paginate(
// 		c,
// 		database.DB.Order("ranking").Model(Article{}),
// 		&articles,
// 		app.V1URL(database.TableName(&Article{})),
// 		perPage,
// 	)
// 	return
// }
